package cn.com.darly.vpm.system.type.impl;

import cn.com.darly.vpm.framework.base.AbstractBaseService;
import cn.com.darly.vpm.framework.base.BaseMapper;
import cn.com.darly.vpm.framework.cache.BaseCache;
import cn.com.darly.vpm.framework.cache.Cache;
import cn.com.darly.vpm.system.type.bean.SystemDic;
import cn.com.darly.vpm.system.type.bean.SystemDicExample;
import cn.com.darly.vpm.system.type.dao.SystemDicMapper;
import cn.com.darly.vpm.system.type.service.SystemDicService;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;

/**
 * Author : ZhangYuHui
 * Date : 2018/4/20
 * TODO :
 */
@Service
public class SystemDicServiceImplementer extends AbstractBaseService<SystemDic,SystemDicExample> implements SystemDicService {
    @Resource
    SystemDicMapper systemDicMapper;

    @Resource
    Cache cache;

    @Override
    protected BaseMapper<SystemDic, SystemDicExample> getBaseMapper() {
        return this.systemDicMapper;
    }

    @Transactional
    public int save(SystemDic dic) {
        int result = this.systemDicMapper.insertSelective(dic);
        this.refreshCache();
        return result;
    }

    @Transactional
    public int deleteByExample(SystemDicExample example) {
        int result = super.deleteByExample(example);
        this.refreshCache();
        return result;
    }

    @Transactional
    public int deleteById(String id) {
        int result = super.deleteById(id, this.getById(id));
        this.refreshCache();
        return result;
    }

    @Transactional
    public int updateByExample(SystemDic record, SystemDicExample example) {
        int result = super.updateByExample(record, example);
        this.refreshCache();
        return result;
    }

    @Transactional
    public int updateByExampleSelective(SystemDic record, SystemDicExample example) {
        int result = super.updateByExampleSelective(record, example);
        this.refreshCache();
        return result;
    }

    @Transactional
    public int updateById(SystemDic record) {
        int result = super.updateById(record);
        this.refreshCache();
        return result;
    }

    @Transactional
    public int updateByIdSelective(SystemDic record) {
        int result = super.updateByIdSelective(record);
        this.refreshCache();
        return result;
    }

    public List<SystemDic> findAll() {
        SystemDicExample search = new SystemDicExample();
        return this.systemDicMapper.selectByExample(search);
    }

    private void refreshCache() {
        cache.put(BaseCache.SYSTEM_CACHE, this.findAll());
    }
}
